/*
 *
 * Copyright Eclub organization.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.eclub.emotion.rootstone.serializer.io;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import com.eclub.emotion.rootstone.util.ByteUtility;

/**
 * 
 *
 * @author   jun.yang, Wincor Nixdorf International GmbH
 * @version  $Revision$
 */
public class RsInput extends ByteArrayInputStream {

	public final int MAX_DATA_LEN = 1024 * 1024 * 10;
	public Charset UTF8_CHARSET = Charset.forName("UTF-8");
	private Map<Integer, Object> _RefPool = new HashMap<Integer, Object>();

	public RsInput(byte[] buffer) {
		super(buffer);
	}

	public RsInput(byte[] buffer, int offset, int length) {
		super(buffer, offset, length);
	}

	public void SafeRead(byte[] buffer) throws IOException {
		if (this.read(buffer) != buffer.length) {
			throw new IOException();
		}
	}

	public Object GetRef(int hashcode) {
		if (hashcode == 0) {
			return null;
		}
		return _RefPool.get(hashcode);
	}

	public void SetRef(int hashcode, Object obj) {
		_RefPool.put(hashcode, obj);
	}

	public short ReadInt16() throws IOException {
		byte[] buffer = new byte[2];
		if (this.read(buffer) != 2) {
			throw new IOException("illegal int16 byte stream");
		}
		return ByteUtility.ToInt16(buffer);
	}

	public int ReadInt32() throws Exception {
		byte[] buffer = new byte[4];
		if (this.read(buffer) != 4) {
			throw new IOException("illegal int32 byte stream");
		}
		return ByteUtility.ToInt32(buffer);
	}

	public long ReadInt64() throws Exception {
		byte[] buffer = new byte[8];
		if (this.read(buffer) != 8) {
			throw new IOException("illegal int64 byte stream");
		}
		return ByteUtility.ToInt64(buffer);
	}

}
